﻿using Common;
using EV.EventBus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Models;

namespace ZoneLine.Stations.HStation
{
    public abstract class HStationBase : WorkStationDBase
    {
        //public string WorkDirectionAddr { get; set; }
        //public string CurrentProductType { get; set; }
        
        //已经移动至基类自定义
        //public void Init(string ip, int port = 102, string startAddr = "DB1000.0", string endUploadCodeAddr = "DB1001.8", string endUploadDataAddr = "DB1001.2", string allowWorkAddr = "DB1001.8")
        //{
        //    Ip = ip;
        //    Port = port;
        //    _startAddr = startAddr;
        //    _readLength = 1028;
        //    StationCode = "OP620";
        //    EndUpLoadCodeAddr = endUploadCodeAddr;
        //    EndUpLoadDataAddr = endUploadDataAddr;
        //    AllowWorkAddr = allowWorkAddr;
        //    HeartToPlcAddr = "DB1001.0";
        //    StationRecordData = new HOP010StationResult();
        //    WorkDirectionAddr = "DB1001.14";
        //    CurrentProductType = "DB1001.2168";
        //    FormulaAddr = "DB1001.32";

        //    TaskMaster.Default.AddCycleTask(nameof(HOP010Station), 1, () =>
        //    {
        //        var value = (short)Cache.WorkDirection;
        //        WriteData(WorkDirectionAddr, value);
        //        var formula = Cache.CurrentFormula.Where(o => o.IP == ip).FirstOrDefault().FormulaNumber;
        //        Console.WriteLine($"{FormulaAddr}{formula}");
        //        //_client.Write(FormulaAddr, formula);
        //        _client.Write(CurrentProductType,Cache.CurrentProductType);

        //    }, false, EV.EventBus.CycleTask.TimeBaseEnum._1S);

        //}

        private HStationResultBase _stationRecordData;

        public HStationResultBase StationRecordData
        {
            get { return _stationRecordData; }
            set => SetProperty(ref _stationRecordData, value);
        }

        protected override void DealData(byte[] content)
        {
            StationRecordData.AbsScanCode = GetPlcString(content, 280);
            StationRecordData.BushScanCode = GetPlcString(content, 322);
            StationRecordData.PartType = GetPlcString(content, 1002);
            StationRecordData.Result = _client.ByteTransform.TransInt16(content, 1000).ToString().Equals("1") ? "OK" : "NG";
            StationRecordData.PressPosition = _client.ByteTransform.TransSingle(content, 1016);
            StationRecordData.PressForce = _client.ByteTransform.TransSingle(content, 1020);
            StationRecordData.CycleTime = _client.ByteTransform.TransSingle(content, 1024);
        }

        //protected override WorkActionEnum Validata(string stationName, string code, string[] otherStations = null)
        //{
        //    var currentStationName = stationName;
        //    var stationIndex = Cache.WorkStationValidationList.IndexOf(currentStationName);
        //    if (stationIndex == -1)
        //    {
        //        return WorkActionEnum.NoEnable;
        //    }
        //    if (stationIndex > 0)
        //    {
        //        //检测上一个站
        //        var beforeStationName = Cache.WorkStationValidationList[stationIndex - 1];
        //        LogWithUi(_logUiChannel, $"在{currentStationName}工位上一个启动的检查工位是{beforeStationName}", code);
        //        var checkRlt = CheckCodeExistAndResult(beforeStationName, code);
        //        LogWithUi(_logUiChannel, $"在{beforeStationName}工位检查{code}的结论是{checkRlt}", code);
        //        if (checkRlt == 1)
        //        {
        //            if (otherStations != null)
        //            {
        //                LogWithUi(_logUiChannel, $"在上个工位{beforeStationName}工位检查{code}的结论是{checkRlt},开始检查校验工位结论", code);
        //                foreach (var otherStation in otherStations)
        //                {
        //                    LogWithUi(_logUiChannel, $"{currentStationName}检查校验工位{otherStation}是否工作", code);
        //                    var otherStationResult = CheckCodeExistAndResult(otherStation, code);
        //                    if (otherStationResult == 1)
        //                    {
        //                        LogWithUi(_logUiChannel, $"{currentStationName}检查校验工位{otherStation}工作过，不在本工位进行工作", code);
        //                        return WorkActionEnum.WorkOkInCheckStation;
        //                    }
        //                }
        //            }
        //            LogWithUi(_logUiChannel, $"在上个工位{beforeStationName}工位检查{code}的结论是{checkRlt},开始检查是否在本工位{currentStationName}结论", code);
        //            var currentResult = CheckCodeExistAndResult(currentStationName, code);
        //            LogWithUi(_logUiChannel, $"在本工位{currentStationName}工位检查{code}的结论是{currentResult}", code);
        //            if (currentResult == 1)
        //            {
        //                return WorkActionEnum.HasWorkInCurrent;
        //            }
        //            else
        //            {
        //                return WorkActionEnum.StartWork;
        //            }
        //        }
        //        else if (checkRlt == 2)
        //        {
        //            return WorkActionEnum.WorkNgInBefore;
        //        }
        //        else
        //        {
        //            return WorkActionEnum.NotWorkInBefore;
        //        }
        //    }
        //    else
        //    {
        //        //检查当前工位
        //        if (otherStations != null)
        //        {
        //            LogWithUi(_logUiChannel, $"在{currentStationName}工位不存在上个工位，检查并行工位工作结论", code);

        //            foreach (var otherStation in otherStations)
        //            {
        //                LogWithUi(_logUiChannel, $"{currentStationName}检查校验工位{otherStation}是否工作", code);
        //                var otherStationResult = CheckCodeExistAndResult(otherStation, code);
        //                if (otherStationResult == 1)
        //                {
        //                    LogWithUi(_logUiChannel, $"{currentStationName}检查校验工位{otherStation}工作过，不在本工位进行工作", code);
        //                    return WorkActionEnum.WorkOkInCheckStation;
        //                }
        //            }
        //        }
        //        LogWithUi(_logUiChannel, $"在{currentStationName}工位不存在上个工位，检查在本工位工作结论", code);
        //        var checkResult = CheckCodeExistAndResult(currentStationName, code);
        //        if (checkResult == 1)
        //        {
        //            return WorkActionEnum.HasWorkInCurrent;
        //        }
        //        else
        //        {
        //            return WorkActionEnum.StartWork;
        //        }
        //    }
        //}
    }
}